<?php
defined('BASEPATH') OR exit('No direct script access allowed');
set_time_limit(0);

class Index extends CI_Controller {

    protected $data;

    public $products;
    public function __construct()
    {
        // Call the CI_Model constructor
        parent::__construct();
        $this->data['base_url'] = $this->config->config['base_url'];
        $this->products = array(
            '1' =>array('product'=>'天气丹华泫水乳6件套','price'=>'1300'),
            '2' =>array('product'=>'拱辰享水妍水乳4件套','price'=>'720'),
            '3' =>array('product'=>'津率享红华凝香6件套','price'=>'900'),
            '4' =>array('product'=>'秘贴自生三合一套装','price'=>'950'),
            '5' =>array('product'=>'拱辰享气韵生6件套','price'=>'960'),
            '6' =>array('product'=>'拱辰享雪玉凝2件套','price'=>'800'),
            '30' =>array('product'=>'坚挺改善装 共2瓶 288元（含运费）','price'=>'288'),
            '31' =>array('product'=>'激情强效装 共5瓶 518元（含运费）','price'=>'518'),
            '32' =>array('product'=>'刚硬强化装 共9瓶 858元（含运费）','price'=>'858'),
            '33' =>array('product'=>'持久巩固装 共14瓶 1188元 (含运费)','price'=>'1188'),
        );
        $this->load->model('event');
        $this->load->library('m/pay');
    }


	public function index()
	{
        $this->load->view('m/index',$this->data);
	}

    public function save()
    {
        $re['code'] = 0;
        $re['data'] = '系统有误请稍后再试！';
        $data = $_POST;

        if(!$this->checkmobile($data['phone']))
        {
            $re['data'] = '手机号码不对';
            echo json_encode($data);
            exit;
        }
        $data['code'] = $this->getcode();
        $data['time'] = time();
        $data= array_merge($data,$this->products[$data['type']]);
        $event = $this->event->insert($data);
        if($event)
        {
            $re['code'] =1;
            $re['url'] = $this->data['base_url']."/index.php/m/index/topay/{$data['code']}";
        }
        echo json_encode($re);
        exit;

    }

    public function topay($code)
    {
        $event = $this->event->get_events_by(array('code' =>$code));
        if(empty($event[0]))
        {
            echo "未找到该订单！";
            return ;
        }
        $data = $event[0];
        $data['return_url'] = $this->data['base_url']."/index.php/m/index/pay/{$data['code']}";
        $data['show_url'] =  $this->data['base_url']."/index.php/m/index/pay/{$data['code']}";
        $this->pay->zhifu($data);
    }

    public function pay($code)
    {
        if(empty($code))
        {
            echo "未找到该订单";
            return ;
        }
        $event = $this->event->get_events_by(array('code' =>$code));
        $this->load->library('m/Pay');
        if(empty($event) && !empty($event[0])) {
            echo "未找到该订单";
            return;
        }elseif($event[0]['is_pay'] == 2){
            echo "已支付！";
            return ;
        }elseif($this->pay->return_url())
        {
            echo "支付失败！";
            error_log(date("[Y-m-d H:i:s]")." -[".$_SERVER['REQUEST_URI']."] :".json_encode($_GET)."\n", 3, "pay_err.log");
            return ;
        }

        $data['out_trade_no'] =  $_GET['out_trade_no'];
        $data['trade_no'] =  $_GET['trade_no'];
        $data['trade_status'] =  $_GET['trade_status'];
        if( $_GET['trade_status'] == 'TRADE_SUCCESS') {
            $data['is_pay'] =  2;
        }
        $data['pay_time'] = time();
        $code= $this->event->update_pay($code,$data);
        if($code)
        {
            if($event[0]['event'] == 3)
            {
                header('location:'.$this->data['base_url'].'/index.php/m/index/bjp');
            }else{
                header('location:'.$this->data['base_url'].'/index.php/m/index');
            }
        }else{
            echo "更新数据库失败";
        }
    }

    public function bjp()
    {
        $this->load->view('m/bjp',$this->data);
    }


    public function getcode()
    {
        return date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
    }

    public function checkmobile($mobile)
    {
        if(preg_match("/^13[0-9]{1}[0-9]{8}$|15[0-9]{1}[0-9]{8}$|17[0-9]{1}[0-9]{8}$|18[0-9]{1}[0-9]{8}$/",$mobile)){
            //验证通过
            return true;
        }else{
            //手机号码格式不对
            return false;
        }
    }

}
